package test.my.com.myapplicationtest.swiperefreshlayout_for_up;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import test.my.com.myapplicationtest.R;

/**
 * Created by ThinkPad on 2016/4/10.
 */
public class RefreshLvLayoutActivity extends Activity{

    private String[] arrays = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "56", "45", "34", "23", "45", "56", "768", "78", "90",};
    private ArrayList<String> arrayList;
    private ListView listView;
    private TestBaseAdapter baseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.aty_refresh_lv_layout);

        arrayList = new ArrayList<String>();

        for (int i = 0; i < arrays.length; ++i) {
            arrayList.add(arrays[i]);
        }
        // 注意, 这是一个ViewGroup类, 不是listview本身
        final RefreshLvLayout refreshLayout = (RefreshLvLayout) findViewById(R.id.swipe_layout);
        // 注意这里的是SwipeRefreshLayout.OnRefreshListener
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {

                Toast.makeText(getApplicationContext(), "on refresh ", Toast.LENGTH_SHORT).show();

                // 如果没有时间消耗,那么加载效果很快消失了
                refreshLayout.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // 刷新完以后调用该方法
                        refreshLayout.setRefreshing(false);
                    }
                }, 1500);
            }
        });

        // 滑动到底部再上拉则加载横多
        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {

            @Override
            public void onLoad() {
                Toast.makeText(getApplicationContext(), "on load ( 加载1.5秒 )", Toast.LENGTH_SHORT)
                        .show();
                // 如果没有时间消耗,那么加载效果很快消失了，相当于没有显示footer视图一样.
                refreshLayout.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // 加载完以后调用该方法
                        refreshLayout.setLoading(false);
                    }
                }, 1500);
            }
        });


//        // listview
        final ListView listView = (ListView) findViewById(R.id.swipe_listview);

        baseAdapter = new TestBaseAdapter();
        listView.setAdapter(baseAdapter);
    }


    private class TestBaseAdapter extends BaseAdapter {
        private TextView textView;

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(RefreshLvLayoutActivity.this).inflate(android.R.layout.simple_list_item_1, null);
                textView = ((TextView) convertView);
                convertView.setTag(textView);
            } else {
                textView = (TextView) convertView.getTag();
            }
            textView.setText(arrayList.get(position));
            return convertView;
        }
    }
}
